import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";

// 引入elementui
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
Vue.use(ElementUI);
// 引入axios
import axios from "axios";
import VueAxios from "vue-axios";
Vue.use(VueAxios, axios);
//引入echarts
import echarts from "echarts";
Vue.prototype.$echarts = echarts;


//引入树形选择器组件
import Treeselect from '@riophae/vue-treeselect'  
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
Vue.component('Treeselect', Treeselect) ;

import { quillEditor } from 'vue-quill-editor'
// 引入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

Vue.use(quillEditor)



// 全局配置axios请求基准地址(跟地址)
axios.defaults.baseURL = "http://8.142.74.15:48082/jeecg-boot/";
// 添加请求拦截器
axios.interceptors.request.use(
  (config) => {
    // 在发送请求之前做些什么
    config.headers['x-access-token'] = sessionStorage.getItem("token");
    return config;
  },
  (error) => {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);
// 添加响应拦截器
axios.interceptors.response.use(
  (response) => {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    if (response.data && response.data.code === 401) {
      ElementUI.Message.error("身份验证过期，请重新登录");
      router.push({ path: '/login' });
    }
    return response;
  },
  (error) => {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    const { response } = error;
    if (response && response.status === 500 && response.data.message.includes("Token失效")) {
      ElementUI.Message.error("Token失效，请重新登录");
      router.push({ path: '/login' });
    } else {
      console.error('请求失败:', error);
      ElementUI.Message.error("请求失败，请稍后再试");
    }
    return Promise.reject(error);
  }
);

//监听dom元素尺寸变化 触发相应的回调的插件
import ElementResizeDetector from "element-resize-detector";
Vue.prototype.$erd = ElementResizeDetector()

//非父子组件通信 借助事件总线eventBus
Vue.prototype.$eventBus = new Vue();




Vue.config.productionTip = false;

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");
